package com.yiren.dbaa.module.practice_set.site.presenter;

import android.app.Activity;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.site.contract.SiteRuleContract;
import com.yiren.dbaa.module.practice_set.site.contract.SiteRuleManageContract;
import com.yiren.dbaa.module.practice_set.site.model.SiteRuleManageModel;
import com.yiren.dbaa.module.practice_set.site.model.SiteRuleModel;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

/**
 * @author lwb
 * @date : 2022/9/30 9:07
 * @description: 考场规则管理P
 */
public class SiteRuleManagePresenter implements SiteRuleManageContract.Presenter {

    private Activity activity;
    private SiteRuleManageContract.Model siteRuleManageModel;
    private SiteRuleContract.Model siteRuleModel;
    private UserParamConfigBean userParamConfigBean;

    public SiteRuleManagePresenter(Activity activity) {
        this.activity = activity;
        siteRuleManageModel = new SiteRuleManageModel(activity);
        siteRuleModel = new SiteRuleModel(activity);
        userParamConfigBean = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void changeUserRange(SiteRuleManageContract.ManageView view, SiteEntity site, int range) {
        siteRuleManageModel.updateUserRange(userParamConfigBean.getUserInfo().getId().toString(), site.getSiteCode(), site.getRuleVer(), range, new CommonCallBack() {
            @Override
            public void success(Object result) {
                //本地修改
                siteRuleManageModel.updateUserRangeLoacl(site, range);
                activity.runOnUiThread(() -> view.updateUseRange(range));
                MessageBox.show(activity, "更改成功!");
            }

            @Override
            public void fail(String error) {
                MessageBox.show(activity, error);
            }
        });
    }

    @Override
    public void updateSiteRule(SiteEntity site, CommonCallBack callBack) {
        LoadDialog.show(activity, "正在更新中...");
        site.setBaseVer(site.getBaseVerLast());
        siteRuleModel.downLoadBaseData(site, new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                site.setBaseVer(site.getBaseVer());
                site.setUpdate(false);
                UserDbController.getInstance(false).update(site);
                callBack.success(site.getBaseVer());
            }

            @Override
            public void fail(String error) {
                LoadDialog.dismiss(activity);
                callBack.fail("");

            }
        });


    }
}
